<template>
    <div class="login-container">
        <el-card class="login-card">
            <template #header>
                <div class="login-header">
                    <h2>系统登录</h2>
                </div>
            </template>

            <el-form ref="loginFormRef" :model="loginForm"  @keyup.enter="handleLogin" :rules="rules">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" clearable />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="Lock" show-password
                        clearable />
                </el-form-item>

                <el-form-item prop="userType">
                    <el-select v-model="loginForm.userType" placeholder="请选择登录类型" style="width: 100%">
                        <el-option label="咨询师" value="1" />
                        <el-option label="管理员" value="2" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" :loading="loading" @click="handleLogin" style="width: 100%">
                        登录
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import axios from 'axios'
const router = useRouter()
const userStore = useUserStore()

const loginForm = ref({
    username: '',
    password: '',
    userType: '1' // 默认为咨询师
})
const rules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
    ],
    userType: [
        { required: true, message: '请选择登录类型', trigger: 'change' }
    ]
}

const loginFormRef = ref()
const loading = ref(false)


const handleLogin = async () => {
    try {
        loading.value = true
        // const response = await axios.post('/api/login', {
        //     username: loginForm.value.username,
        //     password: loginForm.value.password,
        //     userType: loginForm.value.userType
        // })
        // userStore.setToken(response.data.token)
        // userStore.setUserInfo({
        //     username: response.data.username,
        //     userType: response.data.userType
        // })

        ElMessage.success('登录成功')
      console.log(loginForm);

        // 根据用户类型跳转到不同页面
        if (loginForm.value.userType === '1') {
            router.push('/consultant/dashboard')
        } else {
            router.push('/admin/dashboard')
        }
    } catch (error) {
        ElMessage.error( '登录失败，请检查用户名和密码')
    } finally {
        loading.value = false
    }
}
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
}

.login-card {
    width: 400px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
    text-align: center;
    padding: 10px 0;
}

</style>
